import 'dart:convert';

import 'package:flutter_admin/constants/constant.dart';
import 'package:flutter_admin/pages/common/cry/common/application_context.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/model/application.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/dms_file_odoo_api.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/models/odoo/dms_file.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/loading_dialog.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:flutter_admin/utils/utils.dart';

class SaleInquiryFormFileDownLoad extends StatefulWidget {
  String? documentno;

  SaleInquiryFormFileDownLoad({required this.documentno});

  @override
  _SaleInquiryFormFileDownLoadState createState() => _SaleInquiryFormFileDownLoadState();
}

class _SaleInquiryFormFileDownLoadState extends State<SaleInquiryFormFileDownLoad> {
  late List<DmsFile> dmsFileList = [];

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      _loadData();
    });
  }

  _loadData() async{
    ResponseBodyApi res = await DmsFileOdooApi.findByDocumentno(widget.documentno);
    if(res.success!){
      for (int i = 0; i < res.data.length ; i++){
        dmsFileList.add(DmsFile.fromMap(res.data[i]));
      }
      if(dmsFileList.length == 0){
        CryUtils.message("请先上传附件");
        return ;
      }
      setState(() {});
    }
  }

  _download(id){
    //todo/web/content?model=dms.file&id=87&field=content&download=true
    Application application = ApplicationContext.instance.application;
    var odooBaseUrl =application.odooBaseUrl!;
    var data = StoreUtil.read(Constant.CLIENT);
    Utils.launchURL("${odooBaseUrl}web/quninmadexiazaiwenjian/${data['login']}&${data['password']}&$id");
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: dmsFileList.map((dmsFile) =>
          Row(
            children: [
              Expanded(flex: 3,child: CryInput(
                label: '附件名称',
                value: dmsFile.name,
                enable: false,
                onSaved: (v) {
                  dmsFile.name = v;
                },
              ),),
              Expanded(child: CryButton(
                label: "下载",
                onPressed: () {
                  _download(dmsFile.id);
                },
                iconData: Icons.file_download,
              )),
            ],
          )).toList()
      ),
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("附件下载"),
      ),
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            form,
          ],
        ),
      ),
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
